package com.qumitech.visualnovel.service.voiceRoom.impl;

import com.alibaba.fastjson.JSON;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.voiceroom.VoiceRoomSplitFlowCfgDTO;
import com.qumitech.visualnovel.service.voiceRoom.VoiceRoomSplitFlowService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class VoiceRoomSplitFlowServiceImpl implements VoiceRoomSplitFlowService {
    private static final String SPLIT_FLOW_URL_PREFIX = "/voiceroom/console/split/flow/cfg";

    @Override
    public BaseRsp saveSplitFlowCfg(VoiceRoomSplitFlowCfgDTO cfgDTO) {
        String url = UrlConstant.SERVER_URL + SPLIT_FLOW_URL_PREFIX;
        String body = HttpInvokeUtil.httpPostJson(url, JSON.toJSONString(cfgDTO), UrlConstant.DefaultEncode,null);
        return  JSON.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp rmSplitFlowCfg(long id) {
        String url = UrlConstant.SERVER_URL + SPLIT_FLOW_URL_PREFIX + "/id/" + id;
        String body = HttpInvokeUtil.httpDelete(url, new HashMap<>());
        return JSON.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listSplitFlowCfg(String splitFlowShortId) {
        String url = UrlConstant.SERVER_URL + SPLIT_FLOW_URL_PREFIX;
        Map<String, Object> req = new HashMap<>();
        if (!StringUtils.isEmpty(splitFlowShortId)){
            req.put("split_flow_short_id", splitFlowShortId);
        }
        String body = HttpInvokeUtil.httpGet(url, req);
        return  JSON.parseObject(body, BaseRsp.class);
    }
}
